package com.sunstar.koobaselib.kootl.usercenter.model;

import com.socks.library.KLog;
import com.sunstar.koobaselib.data.ApiUrls;
import com.sunstar.koobaselib.kootl.comm.base.BaseCallBack;
import com.sunstar.koobaselib.kootl.comm.okhttp.GsonOkHttpCallback;
import com.sunstar.koobaselib.kootl.comm.okhttp.OkHttpHelper;
import com.sunstar.koobaselib.kootl.comm.okhttp.OkHttpSingleton;
import com.sunstar.koobaselib.kootl.comm.tool.Base64Tool;
import com.sunstar.koobaselib.kootl.comm.tool.MD5Tool;
import com.sunstar.koobaselib.kootl.usercenter.bean.BaseListBean;
import com.sunstar.koobaselib.kootl.usercenter.bean.UserLoginBean;
import com.sunstar.koobaselib.kootl.usercenter.contract.UserLoginContract;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by louisgeek on 2016/12/09
 */

public class UserLoginModelImpl implements UserLoginContract.Model<BaseListBean<UserLoginBean>> {




    @Override
    public void userLoginWeb(String userName, String userPass, final BaseCallBack<BaseListBean<UserLoginBean>> baseCallBack) {

        String versionNum = "1.0";
        String versionStr = Base64Tool.encodeStringToString(versionNum, Base64Tool.BASE64_NO_WRAP);

        final Map<String, String> paramMap = new HashMap<>();
        paramMap.put("version", versionStr);
        paramMap.put("loginid", userName);
        paramMap.put("password", MD5Tool.encode(userPass));
        new OkHttpHelper.Builder().params(paramMap).callback(new GsonOkHttpCallback<BaseListBean<UserLoginBean>>() {
            @Override
            public BaseListBean<UserLoginBean> OnSuccess(String result, int statusCode) {
                return BaseListBean.fromJsonSix(result, UserLoginBean.class);
            }

            @Override
            public void OnSuccessOnUI(BaseListBean<UserLoginBean> baseBean, int statusCode) {
                baseCallBack.onSuccess(baseBean);
            }

            @Override
            public void OnError(String errorMsg, int statusCode) {
                KLog.e("statusCode:" + statusCode + errorMsg);
                String msgmMy="";
                switch (statusCode) {
                    case OkHttpSingleton.OKHTTP_CALL_ERROR:
                        msgmMy="服务器异常";
                        break;
                    case OkHttpSingleton.OKHTTP_CALL_CANCEL:
                        msgmMy="请求取消";
                        break;
                    default:
                        msgmMy="服务器错误";
                }
                baseCallBack.onError(msgmMy);
            }
        }).build().doPostUrl(ApiUrls.userLoginUrl);
    }

    @Override
    public void loadDataBase(String url, int pageNum, int pageSize, String queryKey, BaseCallBack<BaseListBean<UserLoginBean>> baseCallBack) {

    }
}